<template>
	<div class="race-index">
		<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="110px" class="demo-ruleForm">
			<el-form-item prop="name" label="赛事分组名称">
				<el-input  v-model="ruleForm.name" placeholder="请输入赛事分组名称"></el-input>
			</el-form-item>
		</el-form>
		<div style="text-align: center;margin-top: 10px;">
			<el-button type="primary" @click="go2Submit">{{ruleForm.id ? "立即更新" : "立即创建"}}</el-button>
			<el-button @click="go2Cancel">取消</el-button>
		</div>
	</div>
</template>

<script>
	import {
		create,
		update
	} from '@/api/meiyu/raceGroup.js'
	import {
		getToken
	} from '@/utils/auth'

	export default {
		props: {
			originData: {
				type: Object,
				default () {
					return {}
				}
			},
			raceId: {
				type: Number,
				default: undefined
			}
		},
		data() {
			return {
				ruleForm: {
					name: '',
					raceDesc: ""
				},
				rules: {
					name: [{
						required: true,
						message: '请输入赛事分组名称',
						trigger: 'blur'
					}]
				}
			}
		},
		mounted() {
			if (Object.keys(this.originData).length > 0) {
				this.ruleForm = {
					...this.originData
				}
			}
		},
		methods: {
			go2Submit() {
				this.$refs['ruleForm'].validate((valid) => {
					if (valid) {
						if (this.ruleForm.id) {
							// 更新操作
							let req = {
								...this.ruleForm,
								raceId: this.raceId
							}
							update(this.ruleForm.id, req).then((res) => {
								if (res.code === 200) {
									const message = res.message
									this.$message.success('更新成功')
									this.go2Cancel()
								} else {
									this.$message.error(res.msg || '更新失败')
								}
							}).catch((error) => {
								console.log(error)
							}).finally(() => {
								loading.close()
							})
						} else {
							// 添加操作
							let req = {
								...this.ruleForm,
								raceId: this.raceId
							}
							create(req).then((res) => {
								if (res.code === 200) {
									const message = res.message
									this.$message.success('新增成功')
									this.go2Cancel()
								} else {
									this.$message.error(res.msg || '新增失败')
								}
							}).catch((error) => {
								console.log(error)
							}).finally(() => {
								loading.close()
							})
						}
					} else {
						console.log('error submit!!')
						return false
					}
				})
			},
			go2Cancel() {
				this.$emit('close')
			}
		}
	}
</script>

<style lang="scss">
	.race-index {
		padding: 15px;

		.avatar-uploader .el-upload {
			border: 1px dashed #d9d9d9;
			border-radius: 6px;
			cursor: pointer;
			position: relative;
			overflow: hidden;
		}

		.avatar-uploader .el-upload:hover {
			border-color: #409EFF;
		}

		.avatar-uploader-icon {
			font-size: 28px;
			color: #8c939d;
			width: 178px;
			height: 178px;
			line-height: 178px;
			text-align: center;
		}

		.avatar {
			width: 178px;
			height: 178px;
			display: block;
		}
	}
</style>